package com.yh.autobio.modules.adatper;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.ArithUtil;
import com.yh.autobio.component.dialog.DialogHelp;
import com.yh.autobio.modules.bean.PackingBean;
import com.yh.autobio.modules.bean.PackingEntryBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by john on 2018/3/12 0012. 10:14
 */

public class PackingDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context ctx;
    private PackingBean datas;
    private List<PackingEntryBean> products;
    private static final int TYPE_ONE = 0;
    private static final int TYPE_TWO = 1;
    private static final int TYPE_THREE = 2;

    public PackingDetailAdapter(Context ctx) {
        this.ctx = ctx;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE) {
            View view = LayoutInflater.from(ctx).inflate(R.layout.fragment_invoice_head, parent,
                    false);
            return new HeadViewHolder(view);
        } else if (viewType == TYPE_TWO) {
            View view = LayoutInflater.from(ctx).inflate(R.layout.fragment_packing_inverntory, parent,
                    false);
            return new InverntoryViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            initHead((HeadViewHolder) holder);
        } else if (holder instanceof InverntoryViewHolder) {
            initInverntory((InverntoryViewHolder) holder);
        }
    }

    @Override
    public int getItemCount() {
        if (products != null && products.size() > 0) {
            return 1 + products.size();
        } else {
            return 1;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == TYPE_ONE) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    public void setData(PackingBean data) {
        this.datas = data;
    }

    public void setProducts(List<PackingEntryBean> datas) {
        this.products = datas;
        notifyItemChanged(1, datas.size());
    }


    private void initHead(HeadViewHolder holder) {
        holder.tvDjbh.setText(datas.getFBillNo());
//        holder.tvDzxx.setText(datas.getFSite());
        holder.tvFhbm.setText("郑州安图生物工程股份有限公司");
        holder.tvFhrq.setText(datas.getFDate());
//        holder.tvFhsd.setText(datas.getFInterval());
//        holder.tvFyfs.setText(datas.getFCategory());
//        holder.tvKhmc.setText(datas.getFCusName());
        holder.tvShdh.setText(datas.getFTel());
        holder.tvShdw.setText(datas.getFReceive());
        holder.tvShlxr.setText(datas.getFContacts());
        holder.tvShdz.setText(datas.getFLocation());
        holder.tvSjh.setText(datas.getFMobile());
        holder.tvXh.setText(datas.getF_xiang_hao());
    }

    private void initInverntory(final InverntoryViewHolder holder) {
        final int position = holder.getAdapterPosition();
        if (position > 0) {
            final PackingEntryBean bean = products.get(position - 1);


            if (position == 1) {
                holder.llTitle.setVisibility(View.VISIBLE);
            } else {
                holder.llTitle.setVisibility(View.GONE);
            }

            holder.tvCpmc.setText(bean.getFName());
            holder.tvGgxh.setText(bean.getFModel());
            holder.tvSl.setText(ArithUtil.round(Double.parseDouble(bean.getFQty()),2)+"");
            holder.tvBatch.setText(bean.getFBatchNo());
            holder.tvDw.setText(bean.getFUnitName());



        }

    }

    private OnItemLongClickListener listener;

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemLongClickListener {
        void OnItemLong(View view, int position);
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_djbh)
        TextView tvDjbh;
        @InjectView(R.id.tv_shdw)
        TextView tvShdw;
        @InjectView(R.id.ll_gh_unit)
        LinearLayout llGhUnit;
        @InjectView(R.id.tv_fhrq)
        TextView tvFhrq;
        @InjectView(R.id.tv_fhsd)
        TextView tvFhsd;
        @InjectView(R.id.tv_fyfs)
        TextView tvFyfs;
        @InjectView(R.id.tv_khmc)
        TextView tvKhmc;
        @InjectView(R.id.ll_pgno)
        LinearLayout llPgno;
        @InjectView(R.id.tv_fhbm)
        TextView tvFhbm;
        @InjectView(R.id.tv_shlxr)
        TextView tvShlxr;
        @InjectView(R.id.ll_sale)
        LinearLayout llSale;
        @InjectView(R.id.tv_shdh)
        TextView tvShdh;
        @InjectView(R.id.tv_shdz)
        TextView tvShdz;
        @InjectView(R.id.tv_sjh)
        TextView tvSjh;
        @InjectView(R.id.tv_xh)
        TextView tvXh;

        public HeadViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    class InverntoryViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ll_title)
        LinearLayout llTitle;
        @InjectView(R.id.tv_cpmc)
        TextView tvCpmc;
        @InjectView(R.id.tv_ggxh)
        TextView tvGgxh;
        @InjectView(R.id.tv_sl)
        TextView tvSl;
        @InjectView(R.id.tv_batch)
        TextView tvBatch;
        @InjectView(R.id.tv_dw)
        TextView tvDw;
        @InjectView(R.id.ll_sl)
        LinearLayout llSl;
        @InjectView(R.id.ll_content)
        LinearLayout llContent;


        public InverntoryViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
